Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Implements Jupyter AI IPython magics via LangChain. The most common usage syntax looks like this:
Usage
Rapid usage with OpenAI (<5 mins)
First, install the extension.
Then, get an OpenAI API key. You can now use the magics in JupyterLab by starting the server with the
OPENAI_API_KEY
environment variable:Then, in your notebook, load the extension (you must repeat this every time you start a new IPython session):
You can now interact with models via the
%%ai
cell magic.We support maintaining conversation transcripts out-of-the-box for OpenAI Chat models, meaning you can also ask it questions about previous exchanges:
Installation
Obviously, you'll need to install this package first:
LangChain does not list every model provider as an explicit dependency, which means you may have to install them manually before using them. For example, if you are trying to use the HuggingFace providers out-of-the-box, you may encounter the error:
The solution is quite simple; just run
pip install huggingface_hub
and restart your IPython kernel.We list
openai
as an explicit dependency of Jupyter AI, so the OpenAI model providers should work out-of-the-box.Loading the extension
You need to load the extension manually every time you start a new IPython session.
Authentication
Before using models from a model provider, you usually must supply authentication credentials via an environment variable. For example, the
openai
andopenai-chat
providers expect theOPENAI_API_KEY
environment variable to be set. This can be specified when starting the server:Or can be declared in your shell profile file, which is
~/.bash_profile
for Bash users and~/.zprofile
for Zsh users: (note that you will have to restart your shell sessions for this to take effect)Or can be done imperatively inside an IPython notebook cell:
Specifying model ID
A model ID can take three different forms:
langchain.llms
, and their ID is defined under the_llm_type
property of each provider class. Currently we support:ai21, anthropic, cohere, huggingface_hub, huggingface_endpoint, openai, openai-chat, sagemaker_endpoint
openai-chat:gpt-3.5-turbo
gpt-3.5-turbo
chatgpt
is a model alias forgpt-3.5-turbo
When provided with a local model ID, we make a best-effort attempt to infer the provider ID. We do a search through all of the providers we support, and if one provider lists support for the model ID in question, we assume that to be the provider.
openai-chat
supportsgpt-3.5-turbo
, so we assume the provider to beopenai-chat
.Registry providers
Some providers, like are unique in the sense that:
We call these providers "registry providers" (since they mimic the behavior of a package registry, like NPM/PyPi). They are defined internally with the
models
schema field set to[*]
like so:Right now, we have 3 registry providers:
huggingface_hub
,huggingface_endpoint
,sagemaker_endpoint
. For each of these, the model ID has the syntax:huggingface_hub
: the HF repo ID, e.g.gpt2
orgoogle/flan-t5-xxl
huggingface_endpoint
: the URL to the HF endpoint, e.g.https://foo.us-west-2.aws.endpoints.huggingface.cloud/bar
sagemaker_endpoint
: the endpoint name. Your region and authentication credentials should already be specified viaboto3
.We never assume a local model ID to be scoped to a registry provider. That means to use one of these registry providers, you must specify it explicitly by providing a global model ID:
This is because given just the local model ID
google/flan-t5-xxl
, it's ambiguous as to whether this should use thehuggingface_hub
or thesagemaker_endpoint
provider.When given just an HTTP/HTTPS URL, we assume it to be a HuggingFace endpoint, as that is the only supported registry provider that accepts raw URLs as a model ID.
Specifying format
Format currently includes the following:
html, markdown, md, math, json
. The format argument determines what IPython display is used to render output on the notebook. Currently we default tomarkdown
, for whichmd
is a short-hand alias.Prompt interpolation via IPython
By default, any tokens wrapped in curly braces
{}
are replaced with the value of any variable of the same name in the current IPython scope. For example, running:The prompt body is then interpolated to:
before being processed further and sent to the model.
Conversational usage
By default, we maintain a transcript for the
openai-chat
model provider, so the model is forwarded your previous exchanges for context. This means you can ask it questions about previous exchanges.If you wish to discard the transcript for your current IPython session, use the
--reset
flag:Note that we do not currently prune the transcript if it grows excessively large; so you may have to do this manually if you have an extensive number of exchanges within an IPython session.
Known issues
-f/--format
when using%ai
as a line magic triggers an argument parsingUsageError
at runtime:The kernel hangs if the API is rate-limiting the user. There should be an enforced and configurable timeout.
Prompt templates should be inferred per-format. For example, if
--format math
or--format json
are specified, then the prompt template should be something similar to"Perform exclusively the task requested of you. Do not include a description.\n{body}"
.